﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.UI.LabelProviders
{
    class MSGPLabel : AbstractLabelProvider
    {
        public MSGPLabel(string label)
            : base(label)
        {

        }

        public override string GetHtml()
        {
            try
            {
                string[] paras = LabelProviderHelper.GetProperties(this.LabelString);

                if (paras.Length < 1)
                {
                    return this.LabelString;
                }

                string prop = paras[0];
                switch (prop.ToLower())
                {
                    case "url":
                        if (this.Context.State is Message)
                        {
                            return this.GroupLogicFactory.MessageLogic.GetURL(this.Context.State as Message);
                        }
                        else
                        {
                            return this.LabelString;
                        }
                    case "msgtitle":
                        if (this.Context.State is Message)
                        {
                            string value = typeof(Message).GetProperty(prop).GetValue(this.Context.State, null).ToString();
                            if (paras.Length > 1)
                            {
                                int len = 0;
                                if (int.TryParse(paras[1], out len))
                                {
                                    return value.ToShortFormat(len);
                                }
                                else
                                {
                                    return value;
                                }
                            }
                            else
                            {
                                return value;
                            }
                        }
                        else
                        {
                            return this.LabelString;
                        }
                    default:
                        // 如果上下文信息是文章则处理
                        if (this.Context.State is Message)
                        {
                            return typeof(Message).GetProperty(prop).GetValue(this.Context.State, null).ToString();
                        }
                        else
                        {
                            return this.LabelString;
                        }
                }

            }
            catch
            {
                return this.LabelString;
            }
        }
    }
}
